package com.bs.resa.web;

import com.bs.resa.pojo.AdminUser;
import com.bs.resa.pojo.Task;
import com.bs.resa.service.AdminUserService;
import com.bs.resa.service.TaskService;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.text.ParseException;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author lin
 * @date 2019/3/26 11:51
 */
@Controller
public class TaskController {
    private static Logger logger= LoggerFactory.getLogger(TaskController.class);
    protected Map<String, Object> resultMap = new LinkedHashMap<String, Object>();
    @Value("${fastdfs_setting.IMAGE_SERVER_URL}")
    private String url;
    @Autowired
    TaskService taskService;
    @Autowired
    AdminUserService adminUserService;



    @ApiOperation(value = "通过项目id获得任务的甘特图数据",notes = "GET")
    @ResponseBody
    @RequestMapping(value = "project/task/gantetu/{pid}.do",method = RequestMethod.GET)
    public Object queryTaskByPid(@PathVariable("pid")String pid) throws ParseException {
        logger.info("甘特图数据:项目id:"+pid);
        List<Map<String,Object>> taskList=taskService.queryTaskByPidGantetu(pid);
//        System.out.println("甘特数据："+taskList);
        resultMap.put("code",200);
        resultMap.put("data",taskList);

        return taskList;
    }


    @ApiOperation(value = "项目详情-任务视图",notes = "GET")
    @RequestMapping(value = "project/project_table/{pid}.do",method = RequestMethod.GET)
    public ModelAndView projectsTable(ModelAndView model,
                                      @PathVariable("pid")String pid,
                                      @RequestParam(value="taskName" ,required = false)String taskName){
        logger.info("项目id:"+pid);
        logger.info("任务视图");

        List<Map<String,Object>> taskListByPid=taskService.queryTaskByPid(pid,taskName);
//        System.out.println(taskListByPid);
//        List<AdminUser> admin=adminUserService.queryAll();
        List<AdminUser> admin=adminUserService.queryByPid(pid);
        model.addObject("pid",pid);
        model.addObject("admin",admin);

        model.addObject("taskList",taskListByPid);
        model.setViewName("project/project_table");
        return model;
    }


    @ApiOperation(value = "项目详情-添加任务",notes = "POST")
    @ResponseBody
    @RequestMapping(value = "project/insertTask/{pid}.do",method = RequestMethod.POST)
    public Object insertTask(Task task, @PathVariable("pid")String pid) throws ParseException {
        boolean flag=false;

            flag =taskService.insertTaskByPid(task,pid);

        resultMap.put("title","添加任务");
        if(flag){
            logger.info("添加任务成功");
            resultMap.put("code",200);
            resultMap.put("type","success");
            resultMap.put("text","添加任务成功");
        }else{
            resultMap.put("code",500);
            resultMap.put("type","error");
            resultMap.put("text","添加任务失败！请检查");
        }

        return resultMap ;
    }

    @ApiOperation(value = "项目详情-报表图数据",notes = "POST")
    @ResponseBody
    @RequestMapping(value = "project/task/biaobiaoDate/{pid}.do",method = RequestMethod.POST)
    public Object biaobiaoDate( @PathVariable("pid")String pid){
        Map<String,Object> tudate=taskService.getTuDate(pid);

        return tudate;
    }


    @ApiOperation(value = "任务视图-我负责的",notes = "POST")
    @ResponseBody
    @RequestMapping(value = "task/myTask.do",method = RequestMethod.POST)
    public Object myTask(){

        List<Map<String, Object>> data=taskService.queryMytask();

        return data;
    }

    @ApiOperation(value = "任务视图-完成任务数据",notes = "POST")
    @ResponseBody
    @RequestMapping(value = "task/successTask.do",method = RequestMethod.POST)
    public Object successTasks(@RequestParam(value = "taskName",required = false)String taskName){
        System.out.println(taskName);
        List<Map<String, Object>> data=taskService.querySuccTask(taskName);

        return data;
    }
    @ApiOperation(value = "任务视图-更新任务状态",notes = "POST")
    @ResponseBody
    @RequestMapping(value = "task/updataTaskStatus.do",method = RequestMethod.POST)
    public Object updateStatus(String tid,String sid){
        Map<String,Object> result=new HashMap<>();
        try{
           Map<String,Object> flag=taskService.updataStatus(tid,sid);

            result.put("code",200);
            result.put("data",flag);
        }catch (Exception e){
            result.put("code",500);
            result.put("message",e.getMessage());
        }

        return result;
    }


    @ApiOperation(value = "任务视图-查询任务详情",notes = "POST")
    @ResponseBody
    @RequestMapping(value = "task/taskInfo/{tid}.do",method = RequestMethod.POST)
    public Object queryTaskInfo(@PathVariable("tid")String tid){
        resultMap=new HashMap<>();
//        try {
            Map<String,Object> result=taskService.queryTaskByTid(tid);
            resultMap.put("code",200);
            resultMap.put("data",result);
//        }catch (Exception e){
//            resultMap.put("code",500);
//            resultMap.put("message",e.getMessage());
//        }

        return resultMap;
    }


    @ApiOperation(value = "任务视图-更新任务信息",notes = "POST")
    @ResponseBody
    @RequestMapping(value = "/task/update.do",method = RequestMethod.POST)
    public Object updateTeskInfo(String startTime,String endTime,String select,String editor) throws ParseException {
        resultMap=new HashMap<>();
        boolean flag=taskService.updataInfo(startTime,endTime,select,editor);
        if(flag){
            resultMap.put("code",200);
        }else{
            resultMap.put("code",500);
        }
       return  resultMap;
    }


    @ApiOperation(value = "任务视图-更新任务信息",notes = "POST")
    @ResponseBody
    @RequestMapping(value = "task/updateTaskLeibie/{tid}/{lei}.do",method = RequestMethod.POST)
    public Object updateTaskLei(@PathVariable("tid")String tid,@PathVariable("lei")String lei){
        resultMap=new HashMap<>();
        boolean flag=taskService.updataTaskLei(tid,lei);
        if(flag){
            resultMap.put("code",200);

        }else{
            resultMap.put("code",500);
        }
        return  resultMap;

    }


    @ApiOperation(value = "我的任务-新增我的任务",notes = "POST")
    @ResponseBody
    @RequestMapping(value = "task/insertMyTask.do",method = RequestMethod.POST)
    public Object insertMyTask(@RequestParam("descr")String descr,
                               @RequestParam("select")String select,
                               @RequestParam("id")String id) throws ParseException {
        resultMap=new HashMap<>();
//        System.out.println("表单");
//        System.out.println(descr);
//        System.out.println(select);
//        System.out.println(id);


        try {
            Long j=taskService.insertMyTask(descr,select,id);
            if(j!=0){
                Map<String,Object> task=taskService.queryTaskByTid(String.valueOf(j));
                resultMap.put("code",200);
                resultMap.put("data",task);
            }

        }catch (Exception e){
            resultMap.put("code",500);
            resultMap.put("data",null);
            resultMap.put("message",e.getMessage());
        }
        return resultMap;
    }
}
